package com.example.administrator.yuejian2.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;

import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.adapter.QuestionAdapter;
import com.example.administrator.yuejian2.bean.InsertQuestionBean;
import com.example.administrator.yuejian2.bean.MasterBean;
import com.example.administrator.yuejian2.bean.QuestionBean;
import com.example.administrator.yuejian2.model.InsertQuestionModel;
import com.example.administrator.yuejian2.model.MasterDetailModel;
import com.example.administrator.yuejian2.model.QuestionModel;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.example.administrator.yuejian2.other.Url.IMAGEURL;

public class MasterDetailActivity extends BaseActivity {
    private String m_id, u_id;
    private RecyclerView.LayoutManager layoutManager;
    private RetrofitListener<MasterBean> listener;
    private RetrofitListener<InsertQuestionBean> insertQuestionBeanRetrofitListener;
    private RetrofitListener<List<QuestionBean>> listRetrofitListener;
    private QuestionAdapter adapter;
    private ImageView master_detail_pic;
    private SharedPreferences sp;
    private TextView master_detail_name, master_detail_nickname, master_detail_intro, master_detail_attention;
    private RecyclerView recyclerView;

    private Button master_question_button;
    private EditText master_question_title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master_detail);
        initView();
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        u_id = sp.getString("userid", null);
        Intent intent = getIntent();
        m_id = (String) intent.getStringExtra("m_id");
        listener = new RetrofitListener<MasterBean>() {
            @Override
            public void onSuccess(MasterBean masterBean, int flag) {
                master_detail_name.setText(masterBean.getMaster_name());
                master_detail_intro.setText(masterBean.getMaster_intro());
                master_detail_nickname.setText(masterBean.getMaster_nickname());
                master_detail_attention.setText(masterBean.getMaster_attention());
                Picasso.with(MasterDetailActivity.this)
                        .load(IMAGEURL+masterBean.getMaster_pic())
                        .into(master_detail_pic);
            }
            @Override
            public void onFail() {

            }
        };

        listRetrofitListener = new RetrofitListener<List<QuestionBean>>() {
            @Override
            public void onSuccess(List<QuestionBean> questionBeans, int flag) {
                adapter.getQuestionList(questionBeans);
            }

            @Override
            public void onFail() {

            }
        };
        insertQuestionBeanRetrofitListener=new RetrofitListener<InsertQuestionBean>() {
            @Override
            public void onSuccess(InsertQuestionBean insertQuestionBean, int flag) {
                if (insertQuestionBean.getSuccess().equals("1"))
                {
                    Toast.makeText(MasterDetailActivity.this, "提问成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {

            }
        };
        initEvents();
        master_detail();
        getQuestionLIst();
    }


    private void initView() {

        master_detail_pic = findViewById(R.id.master_detail_pic);
        master_detail_attention = findViewById(R.id.master_detail_attention);
        master_detail_intro = findViewById(R.id.master_detail_intro);
        master_detail_name = findViewById(R.id.master_detail_name);

        master_question_button = findViewById(R.id.master_question_button);
        master_question_title = findViewById(R.id.master_question_title);

        master_detail_nickname = findViewById(R.id.master_detail_nickname);
        recyclerView = findViewById(R.id.master_detail_recyclerview);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new QuestionAdapter(this);
        recyclerView.setAdapter(adapter);
    }

    private void initEvents() {
        master_question_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (u_id==null)
                {
                    Toast.makeText(MasterDetailActivity.this,"未登录无法提问大师",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    getInsertQuestion();
                    getQuestionLIst();
                    master_question_title.setText("");
                }

            }
        });
    }

    private void master_detail() {
        MasterDetailModel masterDetailModel = new MasterDetailModel();
        masterDetailModel.getMasterDetail(m_id, listener);
    }
    private void getInsertQuestion() {
        InsertQuestionModel insertQuestionModel = new InsertQuestionModel();
        insertQuestionModel.insertQuestion(u_id, m_id, master_question_title.getText().toString(), insertQuestionBeanRetrofitListener);
    }
    private void getQuestionLIst() {
        QuestionModel questionModel = new QuestionModel();
        questionModel.getQuestionList(m_id, listRetrofitListener);
    }

}
